/**
 * Created by wenchuan.li on 2018/6/14 11:24.
 */
define(function () {

    var self,
        app = win.app,
        userInfo,
        customersArg,
        cdata = {
            GWAK009: '',//客户分类
            GWAK010: '1001',//客户渠道
            GWAK022: '',
            GWAK023: '',
            GWAK024: '',
            GWAK025: '',
            GWAK026: '',
            GWAK028: '',
            GWAK030: '',
            GWAK031: '',
            GWAK050: '',
            GWAK053: '',
            GWAK055: '',
            GWAKL003: '',
            GWAK054: '',//行业二级分类
            GWAK054_1: '',//行业一级分类
            GWAK003: '',
            GWAK004: '',
            GWAK007: '',
            GWAK013: '',
            GWAK014: ''
        },
        provinces = [],
        citys = [],
        districts = [],
        customerFroms = [],
        industrys = [], //行业一级分类
        industrys2 = [] //行业二级分类
    ;

    var JxsAdd = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _readyBefore: function () {
                    customersArg = app.pageParameters['jxsadd_page'];
                }
            }, win);
            userInfo = app.pageModule('p_main').userInfo;
            cdata.GWAK017 = userInfo.id;
            cdata.GWAK018 = userInfo.mobile;
        },
        checkPhone: function () {

            app.ajax({
                xurl: "api/wx/gwat100/checkCustomerPhone",
                loading: true,
                xdata: {
                    GWAK030: cdata.GWAK030
                },
                cb: function (result) {
                    if (result.ok) {
                        var count = result.data.data.count;
                        if (count > 0) {
                            app.alert({
                                msg: '客户手机号已经存在'
                            });
                        } else {
                            self.checkName();
                        }
                    }
                }
            });
        },
        checkName: function () {

            app.ajax({
                xurl: "api/wx/gwat100/checkCustomerName",
                loading: true,
                xdata: {
                    GWAKL004: cdata.GWAKL003
                },
                cb: function (result) {
                    if (result.ok) {
                        var count = result.data.data.count;
                        if (count > 0) {
                            app.alert({
                                msg: '客户名称已经存在'
                            });
                        } else {
                            self.subData();
                        }
                    }
                }
            });
        },
        subData: function () {

            app.ajax({
                // xurl: "sdhn/gwat153/saveJxs",
                xurl: "api/wx/gwat200/insertGwak",
                loading: true,
                xdata: cdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '添加成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                        customersArg.cb();
                    }
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/user/getProvinces",
                // xtime: '2d',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK023 = datas[0].GWAD002;
                        cdata.GWAK022 = datas[0].GWAD003;
                        provinces.pushArray(datas, true);
                        self.getCity();
                    }
                }
            });
        },
        getCity: function () {
            app.ajax({
                xurl: "api/wx/user/getCitys",
                // xtime: '2d',
                xdata: {
                    GWAD003: cdata.GWAK023
                },
                before: function (b) {
                    return Boolean(cdata.GWAK023);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK024 = datas[0].GWAD002;
                            citys.pushArray(datas, true);
                            self.getDistricts();
                        } else {
                            citys.removeAll();
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        getDistricts: function () {
            app.ajax({
                xurl: "api/wx/user/getDistricts",
                // xtime: '2d',
                xdata: {
                    GWAD003: cdata.GWAK024
                },
                before: function (b) {
                    return Boolean(cdata.GWAK024);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK025 = datas[0].GWAD002;
                            districts.pushArray(datas, true);
                        } else {
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        getCustomerTypes: function () {
            app.ajax({
                xurl: "api/wx/gwat200/getGwaf",
                // xtime: '2d',
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK009 = datas[0].GWAF001;
                        customerTypes.pushArray(datas);
                    }
                }
            });
        },
        getCustomerFroms: function () {
            app.ajax({
                xurl: "api/wx/gwat200/getGwae",
                // xtime: '2d',
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK010 = datas[0].GWAE001;
                        customerFroms.pushArray(datas);
                    }
                }
            });
        },
        getIndustry: function () {
            app.ajax({
                xurl: "api/wx/user/getGwbwDatas",
                // xtime: '2d',
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK054_1 = datas[0].GWBW001;
                        industrys.pushArray(datas);
                        self.getIndustry2();
                    }
                }
            });
        },
        getIndustry2: function () { //根据行业一级分类 改变行业二级分类的选项
            app.ajax({
                xurl: "api/wx/user/getGwagDatas",
                // xtime: '2d',
                xdata: {
                    GWAG002: cdata.GWAK054_1
                },
                before: function (b) {
                    return Boolean(cdata.GWAK054_1);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK054 = datas[0].GWAG001;
                            industrys2.pushArray(datas, true);
                        } else {
                            industrys2.removeAll();
                        }
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            self.getProvince();
            self.getIndustry();
            self.getCustomerFroms();
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            new Vue({
                el: doc.querySelector('.mui-content'),
                data: {
                    sub: cdata,
                    provinces: provinces,
                    citys: citys,
                    districts: districts,
                    customerFroms: customerFroms,
                    industrys: industrys, //行业分类
                    industrys2: industrys2, //行业分类
                },
                methods: {
                    provinceChange: function () {
                        self.getCity();
                        provinces.iFilter({
                            o: {GWAD002: cdata.GWAK023},
                            cb: function (arr, len) {
                                if (len > 0) {
                                    cdata.GWAK022 = arr[0].GWAD003;
                                    app.log(cdata.GWAK022);
                                }
                            }
                        });
                    },
                    cityChange: function () {
                        self.getDistricts();
                    },
                    industrysChange: function () {
                        self.getIndustry2();
                    },
                    addData: function () {
                        if (!cdata.GWAKL003 || cdata.GWAKL003 == '') {
                            app.toast('经销商名称不能为空');
                            return;
                        }
                        if (!cdata.GWAK031 || cdata.GWAK031 == '') {
                            app.toast('联系人不能为空');
                            return;
                        }
                        if (!cdata.GWAK030 || cdata.GWAK030 == '') {
                            app.toast('手机号不能为空');
                            return;
                        }
                        if (!cdata.GWAK025 || cdata.GWAK025 == '') {
                            app.toast('区县不能为空');
                            return;
                        }
                        if (!cdata.GWAK054 || cdata.GWAK054 == '') {
                            app.toast('行业分类不能为空');
                            return;
                        }
                        app.confirm({
                            msg: '是否保存经销商信息？',
                            ok: function () {
                                self.checkPhone();
                            }
                        });
                    }
                }
            });
            new Vue({
                el: doc.querySelector('.navbar-mui'),
                data: {},
                methods: {
                    addData: function () {
                        if (!cdata.GWAKL003 || cdata.GWAKL003 == '') {
                            app.toast('经销商名称不能为空');
                            return;
                        }
                        if (!cdata.GWAK031 || cdata.GWAK031 == '') {
                            app.toast('经销商联系人姓名不能为空');
                            return;
                        }
                        if (!cdata.GWAK030 || cdata.GWAK030 == '') {
                            app.toast('手机号不能为空');
                            return;
                        }
                        if (!cdata.GWAK054 || cdata.GWAK054 == '') {
                            app.toast('行业分类不能为空');
                            return;
                        }
                        app.confirm({
                            msg: '是否保存经销商信息？',
                            ok: function () {
                                self.checkPhone();
                            }
                        });
                    }
                }
            });
        }
    });

    new JxsAdd();

});